package org.yx.service.smfr.save_production_order;

import com.xiaoleilu.hutool.collection.CollUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.dbist.dml.Query;
import org.yx.exception.BizException;
import org.yx.models.*;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;

import java.util.ArrayList;
import java.util.List;

/**
 * 保存生产工单信息
 * @author zxw
 * @since 2018/04/19
**/
public class SmfrSaveProductionOrderService extends BaseService {
	public SmfrSaveProductionOrderOut saveProductionOrder(SmfrSaveProductionOrderIn in) throws Exception {
		if (StrUtil.isBlank(in.getMo())) {
			throw new BizException("SYS-0004", new FieldMap("parameter", "mo"));
		}

		SmfrOrdDef order = selectOneLocked(SmfrOrdDef.class, in.getUserOrgCode(), in.getMo());
		if (order == null || new Character('D').equals(order.getMo())) {
			throw new BizException("SMFR-0004", new FieldMap("mo", in.getMo()));
		}

		if (!order.getPathCode().equals(in.getPathCode()) || new Character('Y').equals(in.getReloadPathFlag())) {
			//change process path
			changeProcessPath(order, in.getPathCode());
		}
		copyProperties(in, order);

		update(order);

		SmfrSaveProductionOrderOut out = new SmfrSaveProductionOrderOut();

		return out;
	}

	private void changeProcessPath(SmfrOrdDef order, String pathCode) throws Exception {
		Query queryOper = new Query();
		queryOper.addFilter("orgCode", order.getOrgCode());
		queryOper.addFilter("pathCode", pathCode);
		//查询新工艺路线可用性
		SmfrProPth path = selectOne(SmfrProPth.class, queryOper);
		if (path == null || new Character('D').equals(path.getPathStatus())) {
			throw new BizException("SMFR-0002", new FieldMap("pathCode", pathCode));
		}
		queryOper.addOrder("seqNum", true);

		List<SmfrPthOpr> pathOperList = selectList(SmfrPthOpr.class, queryOper);
		if (CollUtil.isEmpty(pathOperList)) {
			throw new BizException("SMFR-0005", new FieldMap("pathCode", pathCode));
		}

		Query query = new Query(0, 1);
		query.addFilter("orgCode", order.getOrgCode());
		query.addFilter("mo", order.getMo());
		query.addFilter("status", "<>", "D");
		query.addOrder("operSeq", false);

		SmfrPsnSts psnSts = selectOne(SmfrPsnSts.class, query);
		Query query1 = new Query();
		query1.addFilter("orgCode", order.getOrgCode());
		query1.addFilter("mo", order.getMo());
		List<SmfrOrdOpr> oldOrdOprs = selectList(SmfrOrdOpr.class, query1);
		if (psnSts == null) {
			//没有在制记录时，直接插入订单工序表新记录
			if (!CollUtil.isEmpty(oldOrdOprs)) {
				deleteList(SmfrOrdOpr.class, query1);
			}

			List<SmfrOrdOpr> ordOprs = new ArrayList<SmfrOrdOpr>();
			for (SmfrPthOpr pathOpr : pathOperList) {
				SmfrOrdOpr ordOpr = new SmfrOrdOpr();
				copyProperties(pathOpr, ordOpr);
				ordOpr.setMo(order.getMo());
				ordOpr.setOperSeq(pathOpr.getSeqNum());

				ordOprs.add(ordOpr);
			}

			insertBatch(ordOprs);
		} else {
			//有在制记录，判断最远在制品工序之后能否支持修改
			int maxOperSeq = psnSts.getOperSeq();
			for (SmfrOrdOpr ordOpr : oldOrdOprs) {
				for (SmfrPthOpr oper : pathOperList) {
					if (oper.getSeqNum().equals(ordOpr.getOperSeq())) {
						if (!oper.getOper().equals(ordOpr.getOper())) {
							if (oper.getSeqNum().intValue() <= maxOperSeq) {
								//新工序有修改并且在在制之前的工序，不允许修改
								throw new BizException("SMFR-0006", new FieldMap("psn", psnSts.getPsn()), new FieldMap("oper",
										psnSts.getOper()));
							}
						} else {
							ordOpr.setOper(oper.getOper());

							update(ordOpr, "oper");

							pathOperList.remove(oper);
						}
					}
				}
			}

			//如果现在新工艺路线中工序还有剩余不存订单工序中，新增
			if (!CollUtil.isEmpty(pathOperList)) {
				List<SmfrOrdOpr> ordOprs = new ArrayList<SmfrOrdOpr>();
				for (SmfrPthOpr pathOpr : pathOperList) {
					SmfrOrdOpr ordOpr = new SmfrOrdOpr();
					copyProperties(pathOpr, ordOpr);
					ordOpr.setMo(order.getMo());
					ordOpr.setOperSeq(pathOpr.getSeqNum());

					ordOprs.add(ordOpr);
				}

				insertBatch(ordOprs);
			}
		}
	}
}